package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.dto.SysRoleDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.service.ISysRoleService;
import cn.fl.system.transform.SysRoleVOTransform;
import cn.fl.system.vo.SysRoleVO;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysRoleServiceImpl.class);
	
	/**
	 * 权限 API 暴露服务
	 */
	@Autowired
	private IPriorityApiService priorityApiService;
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public SysRoleVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return null;
		SysRoleDTO dto = this.priorityApiService.selectSysRoleById(id);
		return SysRoleVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysRoleVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增角色失败,角色对象为空.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.addSysRole(SysRoleVOTransform.toDTO(entity));
	}

	/*@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		return this.sysRoleApiService.removeById(id);
	}*/

	@Override
	public boolean modify(SysRoleVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改角色信息失败,角色对象为空.DATA:"+JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.modifySysRole(SysRoleVOTransform.toDTO(entity));
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		Pager pager = this.priorityApiService.searchSysRoleListPage(page);
		List<SysRoleDTO> list = (List<SysRoleDTO>)pager.getResultList();
		//加入数据字典
		try{
			if(CollectionUtils.isNotEmpty(list)){
				Map<String, String> roleTypCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_GRP_SYS_ROLE_TYP_CD);
				if(CollectionUtils.isMapNotEmpty(roleTypCdMap)){
					for (SysRoleDTO sysRoleDTO : list) {
						sysRoleDTO.setRoleTypCdNm(roleTypCdMap.get(sysRoleDTO.getRoleTypCd()));
					}
				}
			}
		}catch(Exception e){
			BaseLogUtils.error(logger, "searchListPage", "获取数据字典失败!" + e.getMessage());
		}
		
		pager.setResultList(SysRoleVOTransform.toVOList(list));
		return pager;
		
	}

	@Override
	public boolean modifyEnAbleById(Long id, Integer isValid)
			throws BaseException {
		if(StringUtils.isEmpty(id)||StringUtils.isEmpty(isValid)){
			BaseLogUtils.info(logger, "modifyEnAbleById", "启用角色失败,主键参数或启用标识为空.ID:"+id+",isValid:"+isValid, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.modifyEnAbleSysRole(id, isValid);
	}

	@Override
	public boolean modifyDisAbleById(Long id, Integer isValid)
			throws BaseException {
		if(StringUtils.isEmpty(id)||isValid==null){
			BaseLogUtils.info(logger, "modifyDisAbleById", "禁用角色失败,主键参数或禁用标识为空.ID:"+id+",isValid:"+isValid, CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.priorityApiService.modifyDisAbleSysRole(id, isValid);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPageByOrgId(Pager page) throws BaseException {
		if(page == null) new Pager();
		Pager pager = this.priorityApiService.searchRoleByOrgId(page);
		List<SysRoleDTO> list = (List<SysRoleDTO>)pager.getResultList();
		this.setSysRoleDTOWithDicInfo(list, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		pager.setResultList(SysRoleVOTransform.toVOList(list));
		return pager;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPageNotInOrgId(Pager page) throws BaseException {
		if(page == null) new Pager();
		Pager pager = this.priorityApiService.searchRoleNotInOrgId(page);
		List<SysRoleDTO> list = (List<SysRoleDTO>)pager.getResultList();
		this.setSysRoleDTOWithDicInfo(list, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		pager.setResultList(SysRoleVOTransform.toVOList(list));
		return pager;
	}
	
	/**
	 * 设置系统角色信息中关于字典项信息
	 * @param listDTO
	 */
	private void setSysRoleDTOWithDicInfo(List<SysRoleDTO> listDTO,String sysCd,String orgCd) {
		if(CollectionUtils.isEmpty(listDTO))return;
		try {
			List<String> codeGrpCdList = new ArrayList<>();
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_IS_VALID_CD);
			codeGrpCdList.add(DictionaryConstant.CODE_GRP_SYS_ROLE_TYP_CD);
			Map<String, Map<String, String>>  map = sysDictionaryApiService.searchMultiGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), codeGrpCdList);
			Map<String, String> isValidMap = map.get(DictionaryConstant.CODE_GRP_IS_VALID_CD);
			Map<String, String> roleTypCdMap = map.get(DictionaryConstant.CODE_GRP_SYS_ROLE_TYP_CD);
			for (SysRoleDTO sysRoleDTO : listDTO) {
				//启停
				if(sysRoleDTO.getIsValid()!=null){
					String isValid = sysRoleDTO.getIsValid().toString();
					if(isValidMap.containsKey(isValid)){
						sysRoleDTO.setIsValidNm(isValidMap.get(isValid));
					}
				}
				String roleTypCd = sysRoleDTO.getRoleTypCd();
				if(StringUtils.isNotEmpty(roleTypCd)){
					if(roleTypCdMap.containsKey(roleTypCd)){
						sysRoleDTO.setRoleTypCdNm(roleTypCdMap.get(roleTypCd));
					}
				}
			}
		}catch(Exception e){
			BaseLogUtils.error(logger, "setSysRoleDTOWithDicInfo", "获取数据字典失败!" + e.getMessage());
		}
	}
}
